package com.example.canalclientsync.canal.handler;

import com.example.canalclientsync.canal.mapper.db1.ModelMapper;
import com.example.canalclientsync.canal.po.FileEditUser;
import com.example.canalclientsync.canal.po.VirtualModel;
import com.example.canalclientsync.canal.service.ModelService;
import com.example.canalclientsync.es.document.EsModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
//import top.javatool.canal.client.annotation.CanalTable;
//import top.javatool.canal.client.handler.EntryHandler;

import javax.annotation.Resource;
import java.util.List;

/**
 * 模型编辑者
 *
 * @author chen.cr
 * @since 2023/9/19
 */
//@Component
//@CanalTable("t_file_data_role_record")
@Slf4j
public class FileDataRoleRecordHandler implements EntryHandler<FileEditUser> {

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Resource
    private ModelMapper modelMapper;
    @Resource
    private ModelService modelService;

    @Override
    public void insert(FileEditUser fileEditUser) {
        log.info("insert fileEditUser");
        this.updateRelatedModels(fileEditUser.getMasterId(), fileEditUser.getTenantId());
    }

    @Override
    public void update(FileEditUser before, FileEditUser after) {
        log.info("update fileEditUser");
        this.updateRelatedModels(after.getMasterId(), after.getTenantId());
    }

    @Override
    public void delete(FileEditUser fileEditUser) {
        log.info("delete fileEditUser");
        this.updateRelatedModels(fileEditUser.getMasterId(), fileEditUser.getTenantId());
    }


    /**
     * 编辑者更新时，更新相关模型数据
     *
     * @param masterId
     * @param tenantId
     */
    private void updateRelatedModels(long masterId, String tenantId) {
        List<VirtualModel> models = modelMapper.findByMasterIdAndTenantId(masterId, tenantId);
        for (VirtualModel model : models) {
            EsModel esModel = modelService.buildFromVirtualModel(model);
            if (esModel == null) {
                continue;
            }
            // _id相同，自动覆盖
            elasticsearchRestTemplate.save(esModel);
        }
        log.info("更新 editorUser 相关模型数据完成");
    }


//    /**
//     * 每当 t_file_data_role_record 表有更新，则直接重新查询相关模型的编辑者信息，并更新到es
//     *
//     * @param masterId
//     * @param tenantId
//     */
//    private void updateFileEditUser(long masterId, String tenantId) {
//        List<EsModel> list = esModelRepository.findByMasterIdAndAndTenantId(masterId, tenantId);
//        if (list.size() > 0) {
//
//            // 重新查询编辑者
//            List<FileEditUser> fileEditUsers =
//                    fileDataRoleMapper.listModelEditUserIdByMasterIdAndTenantId(masterId, tenantId);
//            List<Map<String, Object>> mapList = fileEditUsers.stream().map(FileEditUser::toMap).collect(Collectors.toList());
//
//            List<UpdateQuery> uqs = Lists.newArrayListWithExpectedSize(list.size());
//            for (EsModel esModel : list) {
//                Document document = Document.create();
//                document.put("editorUserList", mapList);
//                UpdateQuery updateQuery = UpdateQuery.builder(esModel.getId().toString())
//                                                     .withDocument(document).build();
//                elasticsearchRestTemplate.updateByQuery(updateQuery, EsModel.INDEX);
////                uqs.add(updateQuery);
//            }
////            elasticsearchRestTemplate.bulkUpdate(uqs, EsModel.class);
//            log.info("更新EsModel.editUserList成功");
//        }
//    }

}
